package com.qiyu.datasource.config;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import static com.qiyu.datasource.config.DataSourceConfigRegister.DEFAULT_DATASOURCE;

/**
 * @author idea
 * @data 2020/3/7
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        String dataSource = DataSourceContextHolder.getDatabaseHolder();
        //选择默认数据源
        if (dataSource == null) {
            dataSource = DEFAULT_DATASOURCE.toString();
            DataSourceContextHolder.setDatabaseHolder(DEFAULT_DATASOURCE);
        }
        return dataSource;
    }
}
